package com.lvtongka.app.fragment.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvtongka.app.R
import com.lvtongka.app.databinding.FragmentHomeSmallItemBinding
import com.lvtongka.app.ui.welfare.WelfareItem

class HomeArticleAdapter(private val listener: OnItemFlrListener) : RecyclerView.Adapter<HomeArticleAdapter.BaseHomeHolder>() {

    private var mList: MutableList<WelfareItem> = mutableListOf()

    fun setList(list: List<WelfareItem>) {
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    open inner class BaseHomeHolder(val binding: FragmentHomeSmallItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHomeHolder {
        var binding = FragmentHomeSmallItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return BaseHomeHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: BaseHomeHolder, position: Int) {
        val data = mList[position]
        holder.binding.tvHomeItemTitle.text = data.remark
        holder.binding.ivHomeItemIcon.setImageResource(selectBg(data.type))
        holder.binding.root.setOnClickListener {
            listener.onItemClick(data)
        }
    }

    private fun selectBg(type: String) : Int {
        return when(type) {
            "A"-> R.mipmap.icon_jieriquan
            "B"-> R.mipmap.icon_shengriquan
            "C"-> R.mipmap.icon_shuyingquan
            "D"-> R.mipmap.icon_laobaoquan
            "E"-> R.mipmap.icon_zhunongquan
            else -> R.mipmap.icon_jieriquan
        }
    }

    interface OnItemFlrListener {
        fun onItemClick(item: WelfareItem)
    }
}